package com.briup.sm4;

import com.briup.utils.CalendarUtil;
import org.apache.commons.collections.CollectionUtils;
import org.junit.jupiter.api.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class TestDemo {

    private static final DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("yyyy");

    public static void main(String[] args) throws Exception {
//        test1();
//        System.out.println(getYearWeek("2024-01-02"));
        test3();
        Calendar calendar = Calendar.getInstance();
        System.out.println(Calendar.MARCH == calendar.get(Calendar.MONTH));
//        System.out.println("2023-12-29" +"\t" + test2("2023-12-29"));
//        System.out.println("2023-12-30" +"\t" +test2("2023-12-30"));
//        System.out.println("2024-01-01" +"\t" +test2("2024-01-01"));
//        System.out.println("2024-01-05" +"\t" +test2("2024-01-05"));
//        System.out.println("2024-01-06" +"\t" +test2("2024-01-06"));
//        System.out.println("2024-01-12" +"\t" +test2("2024-01-12"));
//        System.out.println("2024-01-18" +"\t" +test2("2024-01-18"));
//        System.out.println("2024-01-18" +"\t" +test2("2024-02-23"));
    }

    public static void test3() {
        LocalDate localDate = LocalDate.now();
        int monthValue = localDate.getMonthValue();
        int year = localDate.getYear();
        System.out.println(monthValue);
        System.out.println(year);
    }

    public static String test2(String queryDate) {
        LocalDate localDate = LocalDate.parse(queryDate, dayFormatter);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        calendar.setFirstDayOfWeek(Calendar.SATURDAY);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        int weekBeforeNumber = calendar.get(Calendar.WEEK_OF_YEAR);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(sdf.format(calendar.getTime()));
        String week = "";
        if (weekBeforeNumber >= 10) {
            week = week + weekBeforeNumber;
        } else {
            week = week + "0" + weekBeforeNumber;
        }
        String countDate = yearFormatter.format(localDate);
        return countDate + week;
    }

    public static Integer getYearWeek(String Date) {
        java.util.Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(Date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setMinimalDaysInFirstWeek(4);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        int year = calendar.get(Calendar.YEAR);
        calendar.add(Calendar.DATE, -7);//获得上一周的周数
        int lastWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        int lastYear = calendar.get(Calendar.YEAR);
        if (week < lastWeek && year == lastYear) {//周数小于上一周，年数不变说明时间在年末，但是周数已经算到明年，年数需要+1
            year += 1;
        }
        if (week > lastWeek && year > lastYear && lastWeek != 1) {//周数大于上周，年数也大于上周，并且上周不等于1，说明时间在新年的一月，但是周数还是去年的，年数需要-1
            year -= 1;
        }
        return year * 100 + week;
    }

    public static void test1() throws Exception {
        String key = "2da7408c1fe0c3ed3fe31221b51b6b08";
        String mobileNo = "17872380310";
        String encryptEcb = SM4Utils.encryptEcb(key, mobileNo, SM4Utils.ENCODING);
        System.out.println(encryptEcb);
        Set<String> list = new HashSet<>();
        list.add("1");
        list.add("1");
        list.add("2");
        list.add(null);
        list.add("");
        list.add("1");
        System.out.println(list);

        Calendar jobEndTime = Calendar.getInstance();
        jobEndTime.set(Calendar.HOUR_OF_DAY, 15);
        jobEndTime.set(Calendar.MINUTE, 0);
        jobEndTime.set(Calendar.SECOND, 0);
        jobEndTime.set(Calendar.MILLISECOND, 0);
        String time = CalendarUtil.parsefomatCalendar(jobEndTime, CalendarUtil.LONG_FORMAT_LINE);
        System.out.println(time);
        int a = 8 / 0;
    }

    @Test
    public void show12(){
        List<String> aidList = new ArrayList<>();
        aidList.add("aaaaa");
        System.out.println(CollectionUtils.isNotEmpty(aidList));
        SimpleDateFormat ymdHmsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = ymdHmsFormat.format(Calendar.getInstance().getTime());
        System.out.println(format);
    }

    @Test
    public void show13(){
        Calendar instance = Calendar.getInstance();
        int i = instance.get(Calendar.MINUTE);
        System.out.println(i);
    }
}
